import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import home from '../components/home.vue'
import welcome from '../components/Welcome.vue'
import users from '../components/user/users.vue'
import roles from '../components/limit/roles.vue'
import rights from '../components/limit/limits.vue'
import goods from '../components/goods/goods.vue'
import params from '../components/goods/params.vue'
import categories from '../components/goods/categories.vue'
import orders from '../components/order/orders.vue'
import reports from '../components/report/reports.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: welcome },
        { path: '/users', component: users },
        { path: '/roles', component: roles },
        { path: '/rights', component: rights },
        { path: '/goods', component: goods },
        { path: '/params', component: params },
        { path: '/categories', component: categories },
        { path: '/orders', component: orders },
        { path: '/reports', component: reports },
      ]
    }
  ]
})
// 路由权限守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next();
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) return next('/login');
  next();
})

export default router
